class Incumbent < ActiveRecord::Base
  
  has_one :incumbent_evaluation
  
  belongs_to :user, :counter_cache => true
  belongs_to :report_position, :class_name=>"Position", :foreign_key => "position_id"
  belongs_to :view_position, :class_name=>"Position",:foreign_key => "tposition_id"
  belongs_to :report_city, :class_name=>"City",:foreign_key => "city_id"
  belongs_to :view_city, :class_name=>"City",:foreign_key => "tcity_id"
  belongs_to :report_company, :class_name=>"Company",:foreign_key => "company_id"
  belongs_to :view_company, :class_name=>"Company",:foreign_key => "tcompany_id"
  
  has_many :comments,:foreign_key=>"obj_id", :conditions=>"category_id=1"
  has_many :indices, :foreign_key=>"obj_id", :conditions=>"category_id=1"
  
  has_and_belongs_to_many :reports, :class_name=>"Report"
  
  def before_create
    self.created_at=Time.now
    self.updated_at=Time.now
    self.company_report_included=0
    self.city_report_included=0
    self.position_report_included=0
    self.view_times=0
    self.reply_times=0
    self.deleted=0
    self.copy_view_to_report
    self.search_tag<<" "  unless self.search_tag==""
    self.create_search_tag
  end
  
  def after_create
    Index.index_search_tag(self)
  end
  
  def view_sbrank
    return trank.to_f/self.report_company.tgrade.to_f
  end
  
  def report_sbrank
    return rank.to_f/self.report_company.grade.to_f
  end
  
  def report_age
    return user.report_age(self.effective_year)
  end
  
  def report_experience
    return user.report_age(self.effective_year)
  end
  
  def report_sex
    return user.report_sex
  end
  
  def report_education
    return user.report_education
  end
  
  def report_oversea
    return user.report_oversea
  end
   
  def kill
    self.deleted=1
    self.user.incumbents_count -=1
    self.save
  end
  
  def tag_list
    return Index.show_tag_list(self)
  end
  
  def self.validate_params(params,grade,year)
    validation=[]
    validation<<["<li>请输入有效的职位名称</li>"] if params[:tname]==""
    validation<<["<li>输入的职位名称太长了</li>"] if params[:tname].length>100
    validation<<["<li>请输入有效的月均收入</li>"] unless params[:tsalary]=~/^[\d\.]+/&&params[:tsalary].to_f>0
    validation<<["<li>请输入选择有效的所属公司级别</li>"] unless params[:trank]&&params[:trank].to_i.integer? && params[:trank].to_i<=grade.to_i
    validation<<["<li>有效年份必须不小于您工作年份</li>"] unless params[:teffective_year].to_i.integer? && params[:teffective_year].to_i>=year.to_i
    validation<<["<li>请选择所在的城市</li>"] unless params[:tcity_id].to_i.integer? && params[:tcity_id].to_i>0 && params[:tcity_id].to_i<=City.count
    validation<<["<li>请选择所属的职位</li>"] unless params[:tposition_id].to_i.integer? && params[:tposition_id].to_i>0 && params[:tposition_id].to_i<=Position.count
    validation<<["<li>关键字太长了</li>"] if params[:search_tag]&&params[:search_tag].length>200
    return validation
  end
  
  def create_incumbent_report
    self.report_company.report = self.update_company_report
    self.update_city_report
#   self.update_customized_report(self.update_position_report)
    self.update_position_report   
    self.report_company.save
    self.save
  end
  
  def update_city_report
    city_report=Report::CityReport.get_report(self)
    if city_report.new?
      self.city_report_included=1
      self.report_city.incumbents_count +=1
    else
      incumbent.city_report_included=0
    end
    self.save
    self.report_city.save
    city_report.save
    return city_report
  end
  
  def update_company_report
    company_report=Report::CompanyReport.get_report(self)
    if company_report.new?
      self.company_report_included=1
      self.report_company.incumbents_count +=1
    else
      self.company_report_included=0
    end
    self.save
    self.report_company.save
    company_report.save
    return company_report
  end
  
  def update_position_report
    position_report=Report::PositionReport.get_report(self)
    if position_report.new?
      self.position_report_included=1
      self.report_position.incumbents_count +=1
    else
      self.position_report_included=0
    end
    self.update_incumbent_evaluation(position_report)
    self.save
    self.report_position.save
    position_report.save
    return position_report
  end
  
  def update_customized_report(report)
    customized_report=Report::CustomizedReport.create_from_incumbent(self)
    customized_report.copy_from_position_report(report)
    customized_report.save
    return customized_report
  end
  
  def update_incumbent_evaluation(position_report)
    unless position_report.new?
      if position_report.div_salary==0
        self.incumbent_evaluation.confidence= position_report.aver_salary==self.salary ? 10:0
      else
        temp=position_report.aver_salary-self.salary
        temp=temp*temp
        self.incumbent_evaluation.confidence=10*(temp.to_f/position_report.div_salary-1).abs
      end
   else
      self.incumbent_evaluation.confidence=10
   end
   self.incumbent_evaluation.save
  end
  
  def create_search_tag
    self.search_tag << report_company.search_tag + " "    
    self.search_tag << self.name + " "
    self.search_tag << self.report_position.name + " "
    self.search_tag << self.report_city.name
  end
  
  def reindex_search_tag
    self.search_tag<< " "  unless self.search_tag==""
    self.create_search_tag
    Index.clear_search_tag(self)
    Index.index_search_tag(self)
    self.save
  end
  
  def copy_view_to_report
    self.salary=self.tsalary
    self.name=self.tname
    self.position_id=self.tposition_id
    self.city_id=self.tcity_id
    self.company_id=self.tcompany_id
    self.rank=self.trank
    self.effective_year=self.teffective_year  
    self.updated=1
  end
  
  def copy_report_to_view
    self.tsalary=self.salary
    self.tname=self.name
    self.tposition_id=self.position_id
    self.tcity_id=self.city_id
    self.tcompany_id=self.company_id
    self.trank=self.rank
    self.teffective_year=self.effective_year
    self.updated=1
  end
  
  def self.find_age_and_exp(userinfo,ageinfo,expinfo)
    ret=[]
    ret=Incumbent.find(:all,:conditions=>["user_id=?",userinfo[0]])
    len=ageinfo.length
    0.step(len-2, 2) do |i|
      if i==0
        ret.delete_if {|incumbent| incumbent.effective_year.to_i<ageinfo[i]+userinfo[1]}
      elsif i==len-2
        ret.delete_if {|incumbent| incumbent.effective_year.to_i<ageinfo[i]+userinfo[1]&&incumbent.effective_year.to_i>ageinfo[i-1]+userinfo[1]}
        ret.delete_if {|incumbent| incumbent.effective_year.to_i>ageinfo[i+1]+userinfo[1]}
      else
        ret.delete_if {|incumbent| incumbent.effective_year.to_i<ageinfo[i]+userinfo[1]&&incumbent.effective_year.to_i>ageinfo[i-1]+userinfo[1]}
      end
    end
    len=expinfo.length
    0.step(len-2, 2) do |i|
      if i==0
        ret.delete_if {|incumbent| incumbent.effective_year.to_i<expinfo[i]+userinfo[2]}
      elsif i==len-2
        ret.delete_if {|incumbent| incumbent.effective_year.to_i<expinfo[i]+userinfo[2]&&incumbent.effective_yea.to_ir>expinfo[i-1]+userinfo[2]}
        ret.delete_if {|incumbent| incumbent.effective_year.to_i>expinfo[i+1]+userinfo[2]}
      else
        ret.delete_if {|incumbent| incumbent.effective_year.to_i<expinfo[i]+userinfo[2]&&incumbent.effective_year.to_i>expinfo[i-1]+userinfo[2]}
      end
    end
    return ret
  end

  def self.break_int(theparams,interlist)
    ret=[]
    len=interlist.length
    for i in 1...len
      if theparams.include?(i.to_s)
        if ret[-1]==interlist[i-1][0]-1
          ret[-1]=interlist[i-1][1]
        else
          ret += interlist[i-1]
        end
      end
    end
    return ret
  end
  
end
